package com.sillyhat.admin.sqlcreated.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sillyhat.admin.sqlcreated.utils.SqlCreatedUtils;
import com.sillyhat.common.utils.DBUtils;
import com.sillyhat.common.utils.JSONUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/8.
 */
@RequestMapping({"/admin/sqlcreated"})
@Controller
public class SqlCreatedController {

    private Logger logger = Logger.getLogger(SqlCreatedController.class);
    /**
     *跳转数据库配置列表
     * @return
     */
    @RequestMapping("/queryDatabaseList")
    public String queryDatabaseList(){
        return "sysProject/sqlCreated/databasePanel";
    }

    /**
     *跳转数据库配置列表
     * @return
     */
    @RequestMapping("/getDatabaseDetail")
    public String getDatabaseDetail(String id){
        return "sysProject/sqlCreated/databaseForm";
    }


    /**
     * 跳转关于
     * @return
     */
    @RequestMapping("/toSqlCreatedPanel")
    public String toSqlCreatedPanel(){
        return "sysProject/sqlCreated/sqlCreatedPanel";
    }

    @ResponseBody
    @RequestMapping(value="/testConnectionDatabase",method= RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String testConnectionDatabase(String driver,String url,String username,String password){
        Connection conn = new DBUtils(driver,url,username,password).getConnection();
        Map<String,Object> result = Maps.newHashMap();
        if(conn == null){
            result.put("judge",false);
            result.put("result","连接失败");
        }else{
            result.put("judge",true);
            result.put("result","连接成功");
        }
        String json = JSONUtils.mapToJson(result);
        logger.info(json);
        return json;
    }

    @ResponseBody
    @RequestMapping(value="/searchTable",produces = "text/html;charset=UTF-8")
    public String searchTable(HttpServletRequest req, String driver, String url, String username, String password, String tableName){
        Connection conn = new DBUtils(driver,url,username,password).getConnection();
        Map<String,Object> result = Maps.newHashMap();
        String sql = SqlCreatedUtils.getSelectTableSql(tableName);
        List<Map<String,Object>> resultList = Lists.newArrayList();
        List<Map<String,Object>> list = SqlCreatedUtils.queryList(conn,sql);
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> tableMap = list.get(i);
            tableMap.put("ENTITY_FIELD",SqlCreatedUtils.getEntityFieldByTableField(tableMap.get("COLUMN_NAME") != null ? tableMap.get("COLUMN_NAME").toString() : ""));
            resultList.add(tableMap);
        }
        return JSONUtils.listToJson(resultList);
    }

}
